

// Josh made this

#include "Hunter.h"
#include "WindowManager.h"
#include "SFML\System.hpp"

sf::Texture *HunterObject::hunterTex;
bool isDead = false;

void HunterObject::Render()
{
	WindowMan::GetWindow()->draw(hunterSprite);
}

void HunterObject::Update()
{
	static Clock running;
	static Clock dancing;
	static Clock flipping;
	bool isJumping = false;
	bool isRolling = false;
	float deltaT = timer.getElapsedTime().asSeconds();
	timer.restart();
	float t = running.getElapsedTime().asSeconds();

	hunterSprite.setScale(2.f, 2.f);
	Object::Animation(hunterSprite, 0, 0, 39, 48);

	Object::r.top = hunterSprite.getPosition().y ;
	Object::r.left = hunterSprite.getPosition().x;
	Object::r.height = 96.0f;
	Object::r.width = 78.0f;

	// Jumping
	if (Mouse::isButtonPressed(Mouse::Left) && hunterSprite.getPosition().y == 235 && !isDead)
	{
		isJumping = true;
		ySpeed = -5.0f;
		Object::Animation(hunterSprite, 117, 0, 156 - 117, 48);
		Object::r.top = hunterSprite.getPosition().y;
		Object::r.left = hunterSprite.getPosition().x;
		Object::r.height = 96.0f;
		Object::r.width = 78.0f;
	}

	// Rolling / Sliding
	if (Mouse::isButtonPressed(Mouse::Right) && !isDead)
	{
		isRolling = true;
		Object::Animation(hunterSprite, 78, 0, 156 - 117, 48);
		Object::r.top = hunterSprite.getPosition().y + 15;
		Object::r.left = hunterSprite.getPosition().x;
		Object::r.height = 96.0f;
		Object::r.width = 78.0f;
	}

	// Running
	if (t > 0.15f && hunterSprite.getPosition().y == 235 && !isRolling && !isJumping && backgroundSpeed != 0)
	{
		Object::Animation(hunterSprite, 39, 0, 39, 48);

		if (t > 0.4f) running.restart();
	}

	hunterSprite.move(0, ySpeed);
	
	// If pos is below the ground, move back to the ground.
	if (hunterSprite.getPosition().y > 235 && isDead == false) hunterSprite.setPosition(hunterSprite.getPosition().x, 235);

	// Setting the "isDead" variable to true when a collision occurs. This will allow he hunter to fall past the ground.
	if (backgroundSpeed == 0 && caughtTiger == false)
	{
		isDead = true;
	}

	ySpeed += 10.0f * deltaT;

	if (hunterSprite.getPosition().y == 235 && !isDead)
	{
		ySpeed = 0;
		isJumping = false;
	}

	// Dancing!!!!
	if (caughtTiger)
	{
		float danceTimer = dancing.getElapsedTime().asSeconds();
		float flipTimer = flipping.getElapsedTime().asSeconds();

		if (danceTimer > 0.5f)
		{
			Object::Animation(hunterSprite, 117, 0, 156 - 117, 48);

			if (danceTimer > 1.0f) dancing.restart();
		}

		if (flipTimer > 1.0f) hunterSprite.setScale(-2.0f, 2.0f);
		if (flipTimer > 2.0f) flipping.restart();
	}
}